<?php
namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Goodsorder as GoodsOrderModel;
use app\api\model\Member as MemberModel;
use common\util\ArrayUtil;
use web\util\OrderUtil;
use web\util\WebUtil;
use app\common\service\MemberService;

class Team extends  BaseController
{
    
    /*
     * 初始化操作
     */
    public function _initialize() {
        $this->needLogin();
    }
    
    public function lists_zhishu(){//我的直属代理
		
		$mid = $this->getLoginId();
		$list = $this->getSubList($mid);
		$data = array("returnCode"=>1,"returnMsg"=>"success!",'recordsFiltered' => 0,'data' => $list);
		return json($data);
	}
	
	public function lists_fenxiao(){//我的分销代理
		$all = $this->request->param();
		$draw = $this->request->param('draw',1);
		$mid = $this->getLoginId();
	
		$where = ' 1 = 1 ';
		$where .= ' AND fid != ' . $mid . ' AND  FIND_IN_SET('.$mid.', pid_path) ';
			
		$items = MemberModel::instance()->where($where)->order('id','desc')->select();
		$count = MemberModel::instance()->where($where)->count();
		
		$mids = ArrayUtil::getKeyValuesFromArray($items, 'id');
		$res = MemberModel::instance()->whereIn('fid',$mids)->group('fid')->column('count(0)','fid');
		$list =[];
		foreach($items as $item){
		    $id = $item['id'];
		    $subCount = array_key_exists($id, $res) ? $res[$id] : 0;
		    $fenxiaoCount =  MemberModel::instance()->where("FIND_IN_SET($id, pid_path)")->cache(180)->count();
		    $list [] = [
		        'id'=>$id,
		        'avatar'=> get_image($item['face']),
		        'name'=> $item['name'],
		        'direct_count'=> $subCount,//直属
		        'sub_count'=> $fenxiaoCount - $subCount,//分销总数
		    ];
		}
		
		$data = array("returnCode"=>1,"returnMsg"=>"success!", 'recordsTotal' => $count, 'recordsFiltered' => $count, 'draw' => $draw,'data' => $list);
		return json($data);
	}
	
	public static function memberfind($id){
		$data = MemberModel::selectOneMemberByID($id);
		
		$count_zhishu = MemberModel::getCount("fid=".$data['id']);
		$data['count_zhishu'] = $count_zhishu;
		if($count_zhishu > 32){
			$data['count_fenshu'] = $count_zhishu - 32;
		}else{
			$data['count_fenxiao'] = 0;
		}
		
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	public function yejiperson(){
	    $mid = $this->getLoginId();
	    
	    $date = $this->request->param('date',date('Y-m-d'));
	    $date = date('Ymd',strtotime($date));
	    
	    $orderStatuss = OrderUtil::getPayOrderStatus();
	    $where = [];
		$where['ispay'] = ['in',$orderStatuss];
		$where['mid'] = $mid;

		$dayWhere= $date;
		$monthDate= [ 'between',[date('Ym01',time()), date('Ymd', strtotime(date('Y-m-01') . '+1 month -1 day'))]];
		$preMonthDate = [ 'between',[ date('Ymd', strtotime(date('Y-m-01') . '-1 month')), date('Ymd', strtotime(date('Y-m-01') . '-1 day'))]];
		
		$dayWhere = array_merge($where,['date'=>$dayWhere]);
		$monthWhere = array_merge($where,['date'=>$monthDate]);
		$preMonthWhere = array_merge($where,['date'=>$preMonthDate]);
		
		$oneday = GoodsOrderModel::instance()->where($dayWhere)->sum('courier_num');
		$benyue = GoodsOrderModel::instance()->where($monthWhere)->sum('courier_num');
		$shangyue = GoodsOrderModel::instance()->where($preMonthWhere)->sum('courier_num');
		
		$data = [
		    'oneday' => $oneday,//查询一天的业绩
		    'shangyue' => $shangyue,//查询上月的业绩
		    'benyue' => $benyue,//查询本月的业绩
		];
		return $this->returnResult(1,'',$data);
	}
	
	public function yejiteam(){
	    $mid = $this->getLoginId();
	    $mid = $this->getLoginId();
	    
	    $date = $this->request->param('date',date('Y-m-d'));
	    $date = date('Ymd',strtotime($date));
	    
	    $orderStatuss = OrderUtil::getPayOrderStatus();
	    
	    $mids = \app\common\model\MemberModel::instance()->getFMemberIdsByMid($mid);
	    $mids[] = $mid;
	    $where = [];
	    $where['ispay'] = ['in',$orderStatuss];
	    $where['mid'] = ['in',$mids];
	    
		
	    $dayWhere= $date;
	    $monthDate= [ 'between',[date('Ym01',time()), date('Ymd', strtotime(date('Y-m-01') . '+1 month -1 day'))]];
	    $preMonthDate = [ 'between',[ date('Ymd', strtotime(date('Y-m-01') . '-1 month')), date('Ymd', strtotime(date('Y-m-01') . '-1 day'))]];
	    
	    $dayWhere = array_merge($where,['date'=>$dayWhere]);
	    $monthWhere = array_merge($where,['date'=>$monthDate]);
	    $preMonthWhere = array_merge($where,['date'=>$preMonthDate]);
	    
	    $oneday = GoodsOrderModel::instance()->where($dayWhere)->sum('courier_num');
	    $benyue = GoodsOrderModel::instance()->where($monthWhere)->sum('courier_num');
	    $shangyue = GoodsOrderModel::instance()->where($preMonthWhere)->sum('courier_num');
		
		$data = [
		    'oneday' => $oneday,//查询一天的业绩
		    'shangyue' => $shangyue,//查询上月的业绩
		    'benyue' => $benyue,//查询本月的业绩
		];
		return $this->returnResult(1,'',$data);
	}
	
	/**
	 * 获取每一级的子集列表
	 */
	public function sub_list(){
	    
	    $mid = $this->getLoginId();
	    $sub_id  = $this->request->param('sub_id');
	    
	    $memberModel = new \app\common\model\MemberModel();
	    $subMember = $memberModel->find($sub_id);
	    $pid_path = $subMember['pid_path'];
	    $pids = explode(',',$pid_path);
	    if(!in_array($mid, $pids)){
	        return $this->returnResult(-1,'无权限');
	    }
	    $list = $this->getSubList($sub_id);
	    $data = [
	        'id'=>$sub_id,
	        'avatar'=>$subMember['face'],
	        'name'=>$subMember['name'],
	        'grade_name'=>WebUtil::getGradeName($subMember['grade']),
	        'sub_list'=>$list,
	    ];
	    return $this->returnResult(1,'',$data);
	}
	
	/**
	 * 获取pid的子集
	 * @param int $mid
	 */
	private function getSubList($pid){
	    return MemberService::getSubList($pid);
	}
}